package com.huya.controller.admin;

import com.huya.common.result.PageResult;
import com.huya.common.result.Result;
import com.huya.pojo.dto.KnowledgeDTO;
import com.huya.pojo.dto.KnowledgePageQueryDTO;
import com.huya.pojo.entity.Knowledge;
import com.huya.service.KnowledgeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/knowledge")
@Slf4j
public class KnowledgeController {

    @Autowired
    private KnowledgeService knowledgeService;

    /**
     * 添加知识
     * @param
     * @return knowledgeDTO
     */
    @PostMapping()
    public Result add(@RequestBody KnowledgeDTO knowledgeDTO ) {
        knowledgeService.add(knowledgeDTO);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping()
    public Result delete(@RequestParam List<Long> ids)  {
        log.info("delete ids: {}", ids);
        knowledgeService.delete(ids);
        return Result.success();
    }


    /**
     * 分页查询
     * @param knowledgePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(KnowledgePageQueryDTO knowledgePageQueryDTO){
        log.info("knowledgePageQueryDTO: {}", knowledgePageQueryDTO);
        PageResult pageResult = knowledgeService.pageQuery(knowledgePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Knowledge>getById(@PathVariable Long id) {
        log.info("getById: {}", id);
        Knowledge knowledge = knowledgeService.getById(id);
        return Result.success(knowledge);
    }
    /**
     * 更新知识
     * @param knowledgeDTO
     * @return
     */
    @PutMapping()
    public Result update(@RequestBody KnowledgeDTO knowledgeDTO) {
        log.info("update knowledgeDTO: {}", knowledgeDTO);
        knowledgeService.update(knowledgeDTO);
        return Result.success();
    }
}
